package allbutonedivisor;


/**
 * Good match skills
 * @author Qu Huanwen
 *
 */
public class AllButOneDivisor {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		AllButOneDivisor abod = new AllButOneDivisor();
		System.out.println(abod.getMinimum(new int[] { 2, 3, 5 }));
		System.out.println(abod.getMinimum(new int[] { 2, 4, 3, 9 }));
		System.out.println(abod.getMinimum(new int[] { 3, 2, 6 }));
		System.out.println(abod.getMinimum(new int[] { 6, 7, 8, 9, 10 }));
		System.out.println(abod.getMinimum(new int[] { 10, 6, 15 }));
		System.out.println(abod.getMinimum(new int[] { 10, 6 }));
	}

	public int getMinimum(int[] divisors) {
		int num = divisors.length - 1;
		int min = Integer.MAX_VALUE;
		for (int i = 0; i <= num; i++) {
			int t = divisors[num];
			int lcm = lcmAll(divisors, num);
			if (lcm % t != 0 && lcm < min)
				min = lcm;
			divisors[num] = divisors[i];
			divisors[i] = t;
		}
		if (min == Integer.MAX_VALUE)
			return -1;
		else
			return min;
	}

	private int lcmAll(int[] arr, int num) {
		int lcm = arr[0];
		for (int i = 1; i < num; i++) {
			lcm = lcm(lcm, arr[i]);
		}
		return lcm;
	}

	private int gcd(int a, int b) {
		int temp;
		if (a < b) {
			temp = a;
			a = b;
			b = temp;
		}
		while (b > 0) {
			temp = b;
			b = a % b; // % is remainder
			a = temp;
		}
		return a;
	}

	public int lcm(int a, int b) {
		return a * (b / gcd(a, b));
	}
}
